import { setupWorker } from 'msw/browser'
import { Notification } from '@arco-design/web-vue'
import { createUserHandlers } from './user'

export async function enableMock() {
  const prefix = import.meta.env.VITE_API_BASE_URL

  // eslint-disable-next-line no-console
  console.log('%c MOCK ENABLED ', 'color: white;font-weight: bold;background-color: #ce679a;')

  const worker = setupWorker(
    ...[...createUserHandlers(prefix)].map((handler) => {
      handler.log = async () => {}
      return handler
    })
  ).start({
    quiet: true,
    onUnhandledRequest: 'bypass'
  })

  await worker

  if (import.meta.env.VITE_SHOW_MOCK_WARNING === 'true') {
    Notification.warning({
      title: 'Mock Service Worker',
      content: '当前处于开发/测试环境，部分请求将被拦截并返回假数据。',
      duration: 24 * 60 * 60 * 1000,
      closable: true
    })
  }
}
